import java.util.Arrays;

public class LeetCode686 {
    public static void main(String[] args) {
        System.out.println(
                repeatedStringMatch("abcd", "cdabcdab")
        );
    }

    public static int repeatedStringMatch(String a, String b) {
        int aLength = a.length();
        int bLength = b.length();

        if (aLength >= bLength) {
            if (a.contains(b)) {
                return 1;
            }

            if ((a + a).contains(b)) {
                return 2;
            }

        } else {
            int countNum = bLength / aLength + 2;

            StringBuilder sb = new StringBuilder(a);
            for (int i = 2; i <= countNum; i++) {
                sb.append(a);
                if (sb.indexOf(b) != -1) {
                    return i;
                }
            }
        }

        return -1;
    }
}
